<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        :clickModalClose="true"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="formData.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="formData.sex" placeholder="请选择性别" style="width: 100%">
            <el-option label="男" value="1" />
            <el-option label="女" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="学号" prop="studentNum">
          <el-input v-model="formData.studentNum" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="学院" prop="college">
          <el-input v-model="formData.college" placeholder="请输入学院" />
        </el-form-item>
        <el-form-item label="专业" prop="major">
          <el-input v-model="formData.major" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="formData.grade" placeholder="请输入年级，如：2022级" />
        </el-form-item>
        <el-form-item label="头像" prop="cover">
<!--          <el-input v-model="formData.cover" placeholder="请输入头像" />-->
          <div>
            <div>
              <material-picker v-model="formData.cover" :limit="1" />
            </div>
            <div class="form-tips">建议尺寸：240*180px</div>
          </div>
        </el-form-item>
        <el-form-item label="联系电话" prop="contact">
          <el-input v-model="formData.contact" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="比赛方式" prop="playType">
          <el-select class="flex-1" v-model="formData.playType" placeholder="请选择比赛方式">
            <el-option label="男单打" :value=1 />
            <el-option label="女单打" :value=2 />
            <el-option label="男双打" :value=3 />
            <el-option label="女双打" :value=4 />
            <el-option label="男女混打" :value=5 />
          </el-select>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
  competitionSiteDetail,
} from '@/api/competition/competition'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
import {competitionSignUp} from "@/api/member";
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑报名' : '添加报名'
})
const route = useRoute()
const formData = reactive({
  id: '',
  name: '',
  sex: '',
  studentNum: '',
  college: '',
  grade: '',
  major: '',
  cover: '',
  contact: '',
  playType: '',
  schedule: '',
  competitionId: route.query.id
})

const formRules = {
  id: [
    {
      required: true,
      message: '请输入',
      trigger: ['blur']
    }
  ],
  name: [
    {
      required: true,
      message: '请输入名称',
      trigger: ['blur']
    }
  ],
  age: [
        {
          required: true,
          message: '请输入年龄',
          trigger: ['blur']
        }
      ],
  studentNum: [
    {
      required: true,
      message: '请输入学号',
      trigger: ['blur']
    }
  ],
  sex: [
    {
      required: true,
      message: '请选择性别',
      trigger: ['blur']
    }
  ],
  college: [
    {
      required: true,
      message: '请输入学院',
      trigger: ['blur']
    }
  ],
  grade: [
    {
      required: true,
      message: '请输入年级',
      trigger: ['blur']
    }
  ],
  major: [
    {
      required: true,
      message: '请输入专业',
      trigger: ['blur']
    }
  ],
  contact: [
    {
      required: true,
      message: '请输入联系电话',
      trigger: ['blur']
    },
    { pattern: /^1[3456789]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
  ],
  playType: [
    {
      required: true,
      message: '请选择比赛方式',
      trigger: ['blur']
    }
  ]
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  const data: any = { ...formData }
  mode.value == 'edit' ? await competitionSignUp(data) : await competitionSignUp(data)
  popupRef.value?.close()
  // feedback.msgSuccess('操作成功')
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await competitionSiteDetail({
    id: row.id
  })
  setFormData(data)
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
